package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.IdentifyauthorMapper;
import com.hbnu.system.model.entity.Identify;
import com.hbnu.system.model.entity.Identifyauthor;
import com.hbnu.system.model.entity.User;
import com.hbnu.system.service.IIdentifyService;
import com.hbnu.system.service.IIdentifyauthorService;
import com.hbnu.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-05-16
 */
@Service
public class IdentifyauthorServiceImpl extends ServiceImpl<IdentifyauthorMapper, Identifyauthor> implements IIdentifyauthorService {

    @Autowired
    private IIdentifyauthorService iIdentifyauthorService;

    @Autowired
    private IIdentifyService iIdentifyService;

    @Autowired private IUserService iUserService;
    @Override
    public boolean addIdentifyAuthor(Identifyauthor identifyauthor) {
        if (true) { // 此处后续做校外还是校内的判断 再做处理
            if (identifyauthor.getUserAccount().isEmpty()) {
                throw new BaseException("请输入工号");
            }

            User userByAccount = iUserService.getUserByAccount(identifyauthor.getUserAccount());
            if (!(userByAccount.getName().equals(identifyauthor.getName()))) {
                throw new BaseException("输入工号信息与姓名不匹配!");
            }

            Identifyauthor pa1 = iIdentifyauthorService.getOne(Wrappers.lambdaQuery(Identifyauthor.class)
                    .eq(Identifyauthor::getIdentifyID, identifyauthor.getIdentifyID())
                    .eq(Identifyauthor::getUserAccount, identifyauthor.getUserAccount()));

            if (Objects.nonNull(pa1)) {
                throw new BaseException("不可重复添加");
            }
        }
//        }else {
//            if (StrUtil.isEmpty(paperAuthor.getName()) || StrUtil.isBlank(paperAuthor.getName())){
//                throw new BaseException("请输入姓名");
//            }
//
//            Paperauthor pa1 = iPaperauthorService.getOne(Wrappers.lambdaQuery(Paperauthor.class)
//                    .eq(Paperauthor::getPaperID,paperAuthor.getPaperID())
//                    .eq(Paperauthor::getName,paperAuthor.getName()));
//
//            if(Objects.nonNull(pa1)){
//                throw new BaseException("不可重复添加");
//            }
//        }

        Identify identify = iIdentifyService.getOne(Wrappers.lambdaQuery(Identify.class)
                .eq(Identify::getIdentifyID, identifyauthor.getIdentifyID()));
        if(Objects.isNull(identify)){
            throw new BaseException("成果鉴定不存在!");
        }
//        paperAuthor.setSignedunit(paper.getSignedUnit());
        String allauthor = identify.getAllAuthor();
        if(StrUtil.isBlank(allauthor)||StrUtil.isEmpty(allauthor)){
            identify.setAllAuthor(identifyauthor.getName());
        }else if(identify.getAllAuthor().contains(identifyauthor.getName())){
            throw new BaseException("不可重复添加!");
        }else {identify.setAllAuthor(allauthor + "、"+identifyauthor.getName());}
//        if(allauthor.isEmpty()||allauthor.equals(" ")){
//            paper.setAllAuthor(paperAuthor.getName());
//        }else {
//            paper.setAllAuthor(allauthor + "、"+paperAuthor.getName());
//        }
        //把相应论文paper进行修改使用update接口 更新它的相关项目这一字段

        switch (identifyauthor.getAuthorType()){
            case "第一作者":
                identify.setLeadAuthor(identifyauthor.getName());
                break;
            case "通讯作者":
                identify.setCorrespondingAuthor(identifyauthor.getName());
                break;
            case "第二作者":
                identify.setSecondAuthor(identifyauthor.getName());
                break;
            case "第三作者":
                identify.setThirdAuthor(identifyauthor.getName());
                break;
            case "第四作者":
                identify.setFourthAuthor(identifyauthor.getName());
                break;
            case "第五作者":
                identify.setFifthAuthor(identifyauthor.getName());
                break;
            case "第六作者":
                identify.setSixthAuthor(identifyauthor.getName());
                break;
            case "第七作者":
                identify.setSeventhAuthor(identifyauthor.getName());
                break;
            case "第八作者":
                identify.setEighthAuthor(identifyauthor.getName());
                break;
            case "第九作者":
                identify.setNinthAuthor(identifyauthor.getName());
                break;
        }

        UpdateWrapper<Identify> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("IdentifyID",identify.getIdentifyID());
        iIdentifyService.update(identify,updateWrapper);
        return baseMapper.insert(identifyauthor)==1;
    }

    @Override
    public boolean deleteIdentifyAuthor(String id) {
        Identifyauthor identifyAuthor = super.getOne(Wrappers.lambdaQuery(Identifyauthor.class)
                .eq(Identifyauthor::getIdentifyAuthorID, id));
        if(Objects.isNull(identifyAuthor)){
            throw new BaseException("不存在关联作者");
        }

        Identify identify = iIdentifyService.getOne(Wrappers.lambdaQuery(Identify.class)
                .eq(Identify::getIdentifyID, identifyAuthor.getIdentifyID()));
        //相关联的Paper里面的字段也要进行修改 此处先把要删除的项目名称提取出来
        String identifyauthor_name = identifyAuthor.getName();
        //此处把Paper里的相关项目的字段值提取出来
        String allAuthor = identify.getAllAuthor();
        //把项目名替换成空白即可 分三种情况 分别是顿号在前在后和不存在顿号
        String front = "、" + identifyauthor_name;
        String behind = identifyauthor_name + "、";
        String both = "、" + identifyauthor_name + "、";
        if(allAuthor.contains(both)){
            String replace = allAuthor.replace(both, "、");
            identify.setAllAuthor(replace);
        }
        else if(allAuthor.contains(front) && !allAuthor.contains(behind)){
            String replace = allAuthor.replace(front, "");
            identify.setAllAuthor(replace);
        }
        else if (allAuthor.contains(behind) && !allAuthor.contains(front)){
            String replace = allAuthor.replace(behind, "");
            identify.setAllAuthor(replace);
        }
        else if(allAuthor.contains(identifyauthor_name)){
            String replace = allAuthor.replace(identifyauthor_name,"");
            identify.setAllAuthor(replace);
        }

//        if(associated_projects.contains(front)){
//            String replace = associated_projects.replace(front, "");
//            paper.setAssociatedProjects(replace);
//        }
//        if(associated_projects.contains(behind)){
//            String replace = associated_projects.replace(behind, "");
//            paper.setAssociatedProjects(replace);
//        }
//        if (associated_projects.contains(project_name)){
//            String replace = associated_projects.replace(project_name, "");
//            paper.setAssociatedProjects(replace);
//        }

        switch (identifyAuthor.getAuthorType()){
            case "第一作者":
                identify.setLeadAuthor("");
            case "通讯作者":
                identify.setCorrespondingAuthor("");
                break;
            case "第二作者":
                identify.setSecondAuthor("");
                break;
            case "第三作者":
                identify.setThirdAuthor("");
                break;
            case "第四作者":
                identify.setFourthAuthor("");
                break;
            case "第五作者":
                identify.setFifthAuthor("");
                break;
            case "第六作者":
                identify.setSixthAuthor("");
                break;
            case "第七作者":
                identify.setSeventhAuthor("");
                break;
            case "第八作者":
                identify.setEighthAuthor("");
                break;
            case "第九作者":
                identify.setNinthAuthor("");
                break;
        }


        //对paper进行更新
        UpdateWrapper<Identify> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("IdentifyID",identify.getIdentifyID());
        iIdentifyService.update(identify,updateWrapper);

        return baseMapper.deleteById(identifyAuthor) == 1;
    }

    @Override
    public boolean updateIdentifyAuthor(Identifyauthor identifyauthor) {
        return baseMapper.updateById(identifyauthor)==1;
    }

    @Override
    public List<Identifyauthor> pageauthor(String id) {
        List<Identifyauthor> pa = baseMapper.selectList(Wrappers.lambdaQuery(Identifyauthor.class)
                .eq(Identifyauthor::getIdentifyID,id));
        return pa;
    }
}
